home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Resources / Internet / WinHTTrack 3.41-2 / httrack-3.41-2.exe / {app} / src / htsmodules.c < prev    next >
Encoding:
C/C++ Source or Header  |  2007-02-03  |  10.4 KB  |  350 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: htsmodules.c subroutines:                              */
  34. /*       external modules (parsers)                             */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38. /* Internal engine bytecode */
  39. #define HTS_INTERNAL_BYTECODE
  40.  
  41. #include "htsglobal.h"
  42. #include "htsmodules.h"
  43. #include "htsopt.h"
  44. #include "htsbasenet.h"
  45. #include "htslib.h"
  46.  
  47. extern int fspc(httrackp *opt,FILE* fp,const char* type);
  48.  
  49. #ifndef _WIN32
  50. #if HTS_DLOPEN
  51. #include <dlfcn.h>
  52. #endif
  53. #endif
  54.  
  55. /* >>> Put all modules definitions here */
  56. #include "htszlib.h"
  57. #include "htsbase.h"
  58. /* <<< */
  59.  
  60. /* >>> Put all modules variables here */
  61.  
  62. int gz_is_available = 0;
  63. #if 0
  64. t_gzopen gzopen = NULL;
  65. t_gzread gzread = NULL;
  66. t_gzclose gzclose = NULL;
  67. #endif
  68.  
  69. int SSL_is_available = 0;
  70. t_SSL_shutdown SSL_shutdown = NULL;
  71. t_SSL_free SSL_free = NULL;
  72. t_SSL_CTX_ctrl SSL_CTX_ctrl = NULL;
  73. t_SSL_new SSL_new = NULL;
  74. t_SSL_clear SSL_clear = NULL;
  75. t_SSL_set_fd SSL_set_fd = NULL;
  76. t_SSL_set_connect_state SSL_set_connect_state = NULL;
  77. t_SSL_connect SSL_connect = NULL;
  78. t_SSL_get_error SSL_get_error = NULL;
  79. t_SSL_write SSL_write = NULL;
  80. t_SSL_read SSL_read = NULL;
  81. t_SSL_library_init SSL_library_init = NULL;
  82. t_ERR_load_crypto_strings ERR_load_crypto_strings = NULL;
  83. t_ERR_load_SSL_strings ERR_load_SSL_strings = NULL;
  84. t_SSLv23_client_method SSLv23_client_method = NULL;
  85. t_SSL_CTX_new SSL_CTX_new = NULL;
  86. t_ERR_error_string ERR_error_string = NULL;
  87. t_SSL_load_error_strings SSL_load_error_strings = NULL;
  88.  
  89. int V6_is_available = HTS_INET6;
  90.  
  91. static char WHAT_is_available[64]="";
  92. /* <<< */
  93.  
  94. HTSEXT_API const char* hts_get_version_info(httrackp *opt) {
  95.   size_t size;
  96.   int i;
  97.   strcpy(opt->state.HTbuff, WHAT_is_available);
  98.   size = strlen(opt->state.HTbuff);
  99.   for(i = 0 ; i < opt->libHandles.count ; i++) {
  100.     const char *name = opt->libHandles.handles[i].moduleName;
  101.     if (name != NULL) {
  102.       size_t nsize = strlen(name) + sizeof("+");
  103.       size += nsize;
  104.       if (size + 1 >= sizeof(opt->state.HTbuff))
  105.         break;
  106.       strcat(opt->state.HTbuff, "+");
  107.       strcat(opt->state.HTbuff, name);
  108.     }
  109.   }
  110.   return opt->state.HTbuff;
  111. }
  112.  
  113. /* memory checks */
  114. HTSEXT_API htsErrorCallback htsCallbackErr = NULL;
  115. HTSEXT_API int htsMemoryFastXfr = 1;    /* fast xfr by default */
  116. void abortLog__fnc(char* msg, char* file, int line);
  117. void abortLog__fnc(char* msg, char* file, int line) {
  118.   FILE* fp = fopen("CRASH.TXT", "wb");
  119.   if (!fp) fp = fopen("/tmp/CRASH.TXT", "wb");
  120.   if (!fp) fp = fopen("C:\\CRASH.TXT", "wb");
  121.   if (!fp) fp = fopen("CRASH.TXT", "wb");
  122.   if (fp) {
  123.     fprintf(fp, "HTTrack " HTTRACK_VERSIONID " closed at '%s', line %d\r\n", file, line);
  124.     fprintf(fp, "Reason:\r\n%s\r\n", msg);
  125.     fflush(fp);
  126.     fclose(fp);
  127.   }
  128. }
  129. HTSEXT_API t_abortLog abortLog__ = abortLog__fnc;    /* avoid VC++ inlining */
  130.  
  131. static void htspe_log(htsmoduleStruct* str, const char* msg);
  132.  
  133. int hts_parse_externals(htsmoduleStruct* str) {
  134.   str->wrapper_name = "wrapper-lib";
  135.  
  136.   /* External callback */
  137.   if (RUN_CALLBACK1(str->opt, detect, str)) {
  138.     if (str->wrapper_name == NULL)
  139.       str->wrapper_name = "wrapper-lib";
  140.     /* Blacklisted */
  141.     if (multipleStringMatch(str->wrapper_name, StringBuff(str->opt->mod_blacklist))) {
  142.       return -1;
  143.     } else {
  144.       htspe_log(str, str->wrapper_name);
  145.       return RUN_CALLBACK1(str->opt, parse, str);
  146.     }
  147.   }
  148.  
  149.   /* Not detected */
  150.   return -1;
  151. }
  152.  
  153. //static void addCallback(htscallbacks* chain, void* moduleHandle, htscallbacksfncptr exitFnc) {
  154. //  while(chain->next != NULL) {
  155. //    chain = chain->next;
  156. //  }
  157. //  chain->next = calloct(1, sizeof(htscallbacks));
  158. //  assertf(chain->next != NULL);
  159. //  chain = chain->next;
  160. //  memset(chain, 0, sizeof(*chain));
  161. //  chain->exitFnc = exitFnc;
  162. //  chain->moduleHandle = moduleHandle;
  163. //}
  164.  
  165. void clearCallbacks(htscallbacks* chain_);
  166. void clearCallbacks(htscallbacks* chain_) {
  167.   htscallbacks* chain;
  168.   chain = chain_;
  169.   while(chain != NULL) {
  170.     if (chain->exitFnc != NULL) {
  171.       (void) chain->exitFnc();      /* result ignored */
  172.       chain->exitFnc = NULL;
  173.     }
  174.     chain = chain->next;
  175.   }
  176.   chain = chain_;
  177.   while(chain != NULL) {
  178.     if (chain->moduleHandle != NULL) {
  179. #ifdef _WIN32
  180.       FreeLibrary(chain->moduleHandle);
  181. #else
  182.       dlclose(chain->moduleHandle);
  183. #endif
  184.     }
  185.     chain = chain->next;
  186.   }
  187.   chain = chain_->next;     // Don't free the block #0
  188.   while(chain != NULL) {
  189.     htscallbacks* nextchain = chain->next;
  190.     freet(chain);
  191.     chain = nextchain;
  192.   }
  193.   chain_->next = NULL;    // Empty
  194. }
  195.  
  196. void* openFunctionLib(const char* file_) {
  197.   void* handle;
  198.   char *file = malloct(strlen(file_) + 32);
  199.   strcpy(file, file_);
  200. #ifdef _WIN32
  201.   handle = LoadLibraryA(file);
  202.   if (handle == NULL) {
  203.     sprintf(file, "%s.dll", file_);
  204.     handle = LoadLibraryA(file);
  205.   }
  206. #else
  207.   handle = dlopen(file, RTLD_LAZY);
  208.   if (handle == NULL) {
  209.     sprintf(file, "lib%s.so", file_);
  210.     handle = dlopen(file, RTLD_LAZY);
  211.   }
  212. #endif
  213.   freet(file);
  214.   return handle;
  215. }
  216.  
  217. void closeFunctionLib(void* handle) {
  218. #ifdef _WIN32
  219.   FreeLibrary(handle);
  220. #else
  221.   dlclose(handle);
  222. #endif
  223. }
  224.  
  225. void* getFunctionPtr(void* handle, const char* fncname_) {
  226.   if (handle) {
  227.     void* userfunction = NULL;
  228.     char *fncname = strdupt(fncname_);
  229.  
  230.     /* Strip optional comma */
  231.     char *comma;
  232.     if ((comma = strchr(fncname, ',')) != NULL) {   /* empty arg */
  233.       *comma++ = '\0';
  234.     }
  235.     
  236.     /* the function itself */
  237.     userfunction = (void*) DynamicGet(handle, (char*)fncname);
  238.  
  239.     freet(fncname);
  240.  
  241.     return userfunction;
  242.   }
  243.   return NULL;
  244. }
  245.  
  246. void* ssl_handle = NULL;
  247. #ifdef _WIN32
  248. void* ssl_handle_2 = NULL;
  249. #endif
  250. void htspe_init(void) {
  251.   static int initOk = 0;
  252.   if (!initOk) {
  253.     initOk = 1;
  254.     
  255.     /* Zlib is now statically linked */
  256.     gz_is_available = 1;
  257.  
  258.     /* OpenSSL */
  259. #if HTS_DLOPEN
  260.     {
  261.       void* handle;
  262. #ifdef _WIN32
  263.       handle = LoadLibraryA((char*)"ssleay32");
  264. #else
  265.       /* We are compatible with 0.9.6/7/8 and potentially above */
  266.       handle = dlopen("libssl.so.0.9.8", RTLD_LAZY);
  267.       if (handle == NULL) {
  268.         handle = dlopen("libssl.so.0.9.7", RTLD_LAZY);
  269.       }
  270.       if (handle == NULL) {
  271.         handle = dlopen("libssl.so.0.9.6", RTLD_LAZY);
  272.       }
  273.       if (handle == NULL) {
  274.         /* Try harder */
  275.         handle = dlopen("libssl.so", RTLD_LAZY);
  276.       }
  277.       if (handle == NULL) {
  278.         /* Try harder */
  279.         handle = dlopen("libssl.so.0", RTLD_LAZY);
  280.       }
  281. #endif
  282.       ssl_handle = handle;
  283.       if (handle) {
  284.         SSL_shutdown = (t_SSL_shutdown) DynamicGet(handle, (char*)"SSL_shutdown");
  285.         SSL_free = (t_SSL_free) DynamicGet(handle, (char*)"SSL_free");
  286.         SSL_new = (t_SSL_new) DynamicGet(handle, (char*)"SSL_new");
  287.         SSL_clear = (t_SSL_clear) DynamicGet(handle, (char*)"SSL_clear");
  288.         SSL_set_fd = (t_SSL_set_fd) DynamicGet(handle, (char*)"SSL_set_fd");
  289.         SSL_set_connect_state = (t_SSL_set_connect_state) DynamicGet(handle, (char*)"SSL_set_connect_state");
  290.         SSL_connect = (t_SSL_connect) DynamicGet(handle, (char*)"SSL_connect");
  291.         SSL_get_error = (t_SSL_get_error) DynamicGet(handle, (char*)"SSL_get_error");
  292.         SSL_write = (t_SSL_write) DynamicGet(handle, (char*)"SSL_write");
  293.         SSL_read = (t_SSL_read) DynamicGet(handle, (char*)"SSL_read");
  294.         SSL_library_init = (t_SSL_library_init) DynamicGet(handle, (char*)"SSL_library_init");
  295.         ERR_load_SSL_strings = (t_ERR_load_SSL_strings) DynamicGet(handle, (char*)"ERR_load_SSL_strings");
  296.         SSLv23_client_method = (t_SSLv23_client_method) DynamicGet(handle, (char*)"SSLv23_client_method");
  297.         SSL_CTX_new  = (t_SSL_CTX_new) DynamicGet(handle, (char*)"SSL_CTX_new");
  298.         SSL_load_error_strings = (t_SSL_load_error_strings) DynamicGet(handle, (char*)"SSL_load_error_strings");
  299.         SSL_CTX_ctrl = (t_SSL_CTX_ctrl) DynamicGet(handle, (char*)"SSL_CTX_ctrl");
  300. #ifdef _WIN32
  301.         handle = LoadLibraryA((char*)"libeay32");
  302.         ssl_handle_2 = handle;
  303. #endif
  304.         ERR_load_crypto_strings = (t_ERR_load_crypto_strings) DynamicGet(handle, (char*)"ERR_load_crypto_strings");
  305.         ERR_error_string = (t_ERR_error_string) DynamicGet(handle, (char*)"ERR_error_string");
  306.  
  307.         if (SSL_shutdown && SSL_free && SSL_CTX_ctrl && SSL_new && SSL_clear && 
  308.           SSL_set_fd && SSL_set_connect_state && SSL_connect && SSL_get_error && SSL_write 
  309.           && SSL_read && SSL_library_init && SSLv23_client_method && SSL_CTX_new
  310.           && SSL_load_error_strings && ERR_error_string) {
  311.           SSL_is_available = 1;
  312.         }
  313.       }
  314.     }
  315. #endif
  316.     /* */
  317.     
  318.     /* Options availability */
  319.     sprintf(WHAT_is_available, "%s%s%s",
  320.       V6_is_available ? "" : "-noV6",
  321.       gz_is_available ? "" : "-nozip",
  322.       SSL_is_available ? "" : "-nossl");
  323.   }
  324. }
  325.  
  326. void htspe_uninit(void) {
  327. #ifdef _WIN32
  328.   CloseHandle(ssl_handle);
  329.   CloseHandle(ssl_handle_2);
  330.   ssl_handle = NULL;
  331.   ssl_handle_2 = NULL;
  332. #else
  333.   dlclose(ssl_handle);
  334.   ssl_handle = NULL;
  335. #endif
  336. }
  337.  
  338. static void htspe_log(htsmoduleStruct* str, const char* msg) {
  339.   const char* savename = str->filename;
  340.   httrackp* opt = (httrackp*) str->opt;
  341.   if ((opt->debug>1) && (opt->log!=NULL)) {
  342.     HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(External module): parsing %s using module %s"LF, 
  343.       savename, msg);
  344.   }
  345. }
  346.  
  347. HTSEXT_API const char* hts_is_available(void) {
  348.   return WHAT_is_available;
  349. }
  350.